package 线程池.阻塞队列;

import java.util.concurrent.BlockingQueue;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/8/15 11:20
 * @description:
 * @version:
 */
public class Consumer implements Runnable{
    private final BlockingQueue<Task> queue;

    Consumer(BlockingQueue<Task> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        try {
            while (true) {
                Task task = queue.take(); // 从阻塞队列获取任务
                task.execute(); // 执行任务
                Thread.sleep(2000); // 模拟处理任务的耗时
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
